package com.fuqi.android.zhongcai.base.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.flyco.animation.ZoomEnter.ZoomInEnter;
import com.flyco.animation.ZoomExit.ZoomOutExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.application.UniApplication;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.request.api.UnlockApi;
import com.fuqi.android.zhongcai.base.request.result.NullResult;
import com.fuqi.android.zhongcai.base.view.activity.AddWorkerActivity;
import com.fuqi.android.zhongcai.base.view.activity.FeedbackActivity;
import com.fuqi.android.zhongcai.base.view.activity.ForgetPassFirstActivity;
import com.fuqi.android.zhongcai.base.view.activity.LoginActivity;
import com.fuqi.android.zhongcai.base.view.activity.TrainingActivity;
import com.fuqi.android.zhongcai.base.view.widget.HashView;
import com.fuqi.android.zhongcai.business.shuig.request.api.CheckInfoQueryByDateApi;
import com.fuqi.android.zhongcai.business.shuig.request.api.OrderDataApi;
import com.fuqi.android.zhongcai.business.shuig.request.result.CheckInfoQueryByDateResult;
import com.fuqi.android.zhongcai.business.shuig.request.result.OrderDataResult;
import com.fuqi.android.zhongcai.business.shuig.view.activity.MonthChouChaActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.MonthPerOrderListActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.OrderSearchListActivity;
import com.fuqi.android.zhongcai.business.shuig.view.activity.TodayOrderListActivity;
import com.fuqi.android.zhongcai.sql.dao.SQLDao;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.TimeUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import butterknife.Bind;
import butterknife.OnClick;

import static com.fuqi.android.zhongcai.R.id.rl_today_order;
import static com.fuqi.android.zhongcai.R.id.rl_top;

/**
 * Created by juner on 2016/8/29.
 */
public class UserCenterFragment extends BaseFragment {

    @Bind(R.id.user_hashview)
    HashView hashView;
    @Bind(R.id.rl_today_order)
    RelativeLayout rlToday;
    @Bind(R.id.rl_top)
    RelativeLayout rlTop;
    @Bind(R.id.tv_clear_cache)
    TextView tvClearCache;
    @Bind(R.id.tv_top)
    TextView tvTop;
    @Bind(R.id.tv_modfy_pwd)
    TextView tvModfyPwd;
    @Bind(R.id.rl_unlock)
    RelativeLayout rlUnlock;

    private static UserCenterFragment fragment;

    public static UserCenterFragment getInstance(){
        if(fragment == null) fragment = new UserCenterFragment();
        return fragment;
    }

    @OnClick({R.id.iv_header, rl_top,rl_today_order,R.id.rl_modfy_pwd,R.id.rl_clear_memery,R.id.rl_unlock})
    public void onClick(View view){


        switch (view.getId()){

            case R.id.iv_header:
                ActivityUtils.startActivity(getActivity(), LoginActivity.class);
                getActivity().finish();
                System.exit(0);
                break;
            case rl_today_order:
                // 当日订单
                ActivityUtils.startActivity(getActivity(), TodayOrderListActivity.class);
                break;
            case rl_top:
                if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
                    // 反馈建议
                    ActivityUtils.startActivity(getActivity(), FeedbackActivity.class);

                }else if(UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
                    // 订单查询
                    ActivityUtils.startActivity(getActivity(), OrderSearchListActivity.class);
                }else if(UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
                    // 添加工人
                    ActivityUtils.startActivity(getActivity(), AddWorkerActivity.class);

                }else if(UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
                    // 培训交流
                    ActivityUtils.startActivity(getActivity(), TrainingActivity.class);

                }
                break;
            case R.id.rl_modfy_pwd:
//                ActivityUtils.startActivity(getActivity(), ForgetPassFirstActivity.class);
              //修改建议  此处需要判断登录，
//                ActivityUtils.startActivity(getActivity(), ContactUsActivity.class);
                ActivityUtils.startActivity(getActivity(), ForgetPassFirstActivity.class);
                break;
            case R.id.rl_clear_memery:
                showClearDialog();
                break;
            case R.id.rl_unlock:
                showUnlockDialog();
                break;
        }

    }



    public void showClearDialog() {
        final NormalDialog dialog = new NormalDialog(getActivity());
        dialog.setCanceledOnTouchOutside(false);
        dialog.content(" 确定要清除缓存吗?")
                .style(NormalDialog.STYLE_TWO)
                .title("")
                .titleTextSize(0)
                .contentTextSize(14)
                .contentTextColor(Color.parseColor("#444444"))
                .contentGravity(Gravity.CENTER)
                .showAnim(new ZoomInEnter())
                .dismissAnim(new ZoomOutExit())
                .cornerRadius(8)
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {

                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        UniApplication.getInstance().deleteUserInfo();
                        SQLDao.getInstance(getActivity().getApplicationContext()).deleteAll();
                        ActivityUtils.startActivity(getActivity(),LoginActivity.class);
                        getActivity().finish();
                        System.exit(0);
                        dialog.dismiss();

                    }
                });

    }

    public void showUnlockDialog() {
        final NormalDialog dialog = new NormalDialog(getActivity());
        dialog.setCanceledOnTouchOutside(false);
        dialog.content(" 确定要解除绑定吗?")
                .style(NormalDialog.STYLE_TWO)
                .title("")
                .titleTextSize(0)
                .contentTextSize(14)
                .contentTextColor(Color.parseColor("#444444"))
                .contentGravity(Gravity.CENTER)
                .showAnim(new ZoomInEnter())
                .dismissAnim(new ZoomOutExit())
                .cornerRadius(8)
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {

                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        RequestManager.getInstance().call(new UnlockApi(new UnlockApi.UnlockParams(), respUnlockListener, errorListener));
                        dialog.dismiss();


                    }
                });

    }

    Response.Listener<NullResult> respUnlockListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {

            if (HandleCode.requestSuccess()) {
                Log.i("juner", "juner response respUnlockListener: " + GsonHelper.writeValue(response));
            } else {}
        }
    };


    @Override
    public void initView() {

        if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
            // 用户
            hashView.setVisibility(View.GONE);
            tvTop.setText("用户反馈");
            rlToday.setVisibility(View.GONE);
        } else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
            // 施工工人
            tvTop.setText("培训交流");
            rlToday.setVisibility(View.GONE);
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
            // 监察员
            rlTop.setVisibility(View.GONE);
            rlToday.setVisibility(View.GONE);
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
            // 内勤
            tvTop.setText("订单查询");
            rlToday.setVisibility(View.GONE);
//            rlToday.setVisibility(View.GONE);
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
            // 施工队长
            tvTop.setText("添加工人");

        }


    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

//        tvClearCache.setText("清除缓存" + "(" + DataCleanManager.getTotalCacheSize(getActivity()) + ")");
            Log.e("ccqx","CurrentRole"+UniApplication.getInstance().getCurrentRole());
        if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
            // 用户

        } else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGGR)){
            // 施工工人
            RequestManager.getInstance().call(new OrderDataApi(new OrderDataApi.OrderDataParams(TimeUtils.getMonthFirstDayDate(),
                    TimeUtils.getTommorwDate()), respUserCenterListener, errorListener));
        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
            // 监察员
            RequestManager.getInstance().call(new CheckInfoQueryByDateApi(new CheckInfoQueryByDateApi.CheckInfoQueryByDateParams(TimeUtils.getMonthFirstDayDate(),
                    TimeUtils.getTommorwDate()), respJCYListener, errorListener));

        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_NQ)){
            // 内勤
            RequestManager.getInstance().call(new OrderDataApi(new OrderDataApi.OrderDataParams(TimeUtils.getMonthFirstDayDate(),
                    TimeUtils.getTommorwDate()), respUserCenterListener, errorListener));

        }else if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_SGDZ)){
            // 施工队长
            RequestManager.getInstance().call(new OrderDataApi(new OrderDataApi.OrderDataParams(TimeUtils.getMonthFirstDayDate(),
                    TimeUtils.getTommorwDate()), respUserCenterListener, errorListener));
        }


    }

    Response.Listener<OrderDataResult> respUserCenterListener = new Response.Listener<OrderDataResult>() {

        @Override
        public void onResponse(OrderDataResult response) {

            if (HandleCode.requestSuccess()) {

                createHashViewList(response);

                Log.i("juner", "juner respListener response : ");
            } else {



            }
        }
    };

    Response.Listener<CheckInfoQueryByDateResult> respJCYListener = new Response.Listener<CheckInfoQueryByDateResult>() {

        @Override
        public void onResponse(CheckInfoQueryByDateResult response) {

            if (HandleCode.requestSuccess()) {

                createHashViewListJCY(response);

                Log.i("juner", "juner response respJCYListener: " + GsonHelper.writeValue(response));
            } else {



            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };




    private void createHashViewList(OrderDataResult orderDataResult) {

        hashView.clear();
        hashView.setVisibility(View.VISIBLE);
        hashView.setClickListener(listener);
        hashView.addBasicItem("月安装套数", orderDataResult.getAzCount());
        hashView.addBasicItem("月改装套数", orderDataResult.getGzCount());
        hashView.addBasicItem("改装率", orderDataResult.getAzl());
        hashView.addBasicItem("客户好评率", orderDataResult.getCommentScore());
//        hashView.addBasicItem("改装率", StringUtil.getPercent(StringUtil.parseDouble(orderDataResult.getAzl()),100));
//        hashView.addBasicItem("客户好评率", StringUtil.getPercent(StringUtil.parseDouble(orderDataResult.getCommentScore()),100));
        hashView.commit();

        hashView.setClickListener(listener);

    }

    private void createHashViewListJCY(CheckInfoQueryByDateResult checkInfoQueryByDateResult) {

        hashView.clear();
        hashView.setVisibility(View.VISIBLE);
        hashView.setClickListener(listener);
        hashView.addBasicItem("月抽查套数(套)", String.valueOf(checkInfoQueryByDateResult.getCheckCount()));
        hashView.addBasicItem("月罚单套数(套)", String.valueOf(checkInfoQueryByDateResult.getPunishCount()));
        hashView.addBasicItem("月罚单金额(元)", String.valueOf(checkInfoQueryByDateResult.getAmount()));
        hashView.commit();

        hashView.setClickListener(listener);
    }


    private HashView.ClickListener listener = new HashView.ClickListener() {

        @Override
        public void onClick(int index) {
            Bundle bundle = null;
            if (UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_GRKH)){
                // 用户

            }else if(UniApplication.getInstance().getCurrentRole().equals(Constants.ROLE_JCY)){
                // 监察员
                switch (index) {

                    case 0:
                        bundle = new Bundle();
                        bundle.putString("title","当月抽查");
                        bundle.putString("order_type",Constants.MONTH_CHOU_CHA);
                        ActivityUtils.startActivityNeedLogin(getActivity(), MonthChouChaActivity.class,bundle);
                        break;

                    case 1:
                        bundle = new Bundle();
                        bundle.putString("title","当月罚单");
                        bundle.putString("order_type",Constants.MONTH_FA_DAN);
                        ActivityUtils.startActivityNeedLogin(getActivity(), MonthChouChaActivity.class,bundle);
                        break;

                    case 2:
                        break;

                }

            }else {
                switch (index) {

                    case 0:
                        bundle = new Bundle();
                        bundle.putString("title","当月安装");
                        bundle.putString("order_type",Constants.AN_ZHUNG);
                        ActivityUtils.startActivityNeedLogin(getActivity(), MonthPerOrderListActivity.class,bundle);
                        break;

                    case 1:
                        bundle = new Bundle();
                        bundle.putString("title","当月改装");
                        bundle.putString("order_type",Constants.GAI_ZHUANG);
                        ActivityUtils.startActivityNeedLogin(getActivity(), MonthPerOrderListActivity.class,bundle);
                        break;
                    case 2:
//                        bundle.putString("title","改装率");
//                        ActivityUtils.startActivityNeedLogin(getActivity(), MonthPerOrderListActivity.class);
                        break;
                    case 3:
//                        bundle.putString("title","好评率");
//                        ActivityUtils.startActivityNeedLogin(getActivity(), MonthPerOrderListActivity.class);
                        break;

                }

            }


        }
    };


    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_center;
    }
}
